package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.AgencyGrantRecord;
import com.jxc.model.po.Brand;
import com.jxc.model.po.DealerGrantRecord;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.VAgencyGrantRecord;
import com.jxc.model.vo.VDealerGrantRecord;
import com.jxc.model.vo.VUser;
import com.jxc.service.AgencyServiceI;
@Service("agencyService")
public class AgencyServiceImpl implements AgencyServiceI {
	@Autowired
	private BaseDaoI<User> userDao;
	
	@Autowired
	private BaseDaoI<DealerGrantRecord> dealerDao;

	@Override
	public DataGrid datagrid(VDealerGrantRecord vDealerGrantRecord) {
		DataGrid dg = new DataGrid();
		List<VUser> vu = new ArrayList<VUser>();
		try{
			List<VDealerGrantRecord> vdgrs = this.changeModel(this.find(vDealerGrantRecord));
			for(VDealerGrantRecord vdgr : vdgrs){
				VUser vuser = new VUser();
				BeanUtils.copyProperties(vdgr, vuser);
				vuser.setDgrId(vdgr.getId());
				User u = userDao.get(User.class, vdgr.getAgencyid());
				BeanUtils.copyProperties(u, vuser);
				
				List<Brand>  vbs = u.getBrands();
				String brid = "";
				String brnm = "";
				for(Brand b:vbs){
					if(b.getIsDelete()!=true){
						brid += b.getId()+",";
						brnm += b.getName()+",";
					}
				}
				if(brid.length()>1){
					vuser.setBrandids(brid.substring(0, brid.length()-1));
					vuser.setBrandname(brnm.substring(0, brnm.length()-1));
				}
				vu.add(vuser);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		dg.setRows(vu);
		dg.setTotal(this.total(vDealerGrantRecord));
		return dg;
	}

	private List<VDealerGrantRecord> changeModel(List<DealerGrantRecord> dealers) {
		List<VDealerGrantRecord> vdealergrs = new ArrayList<VDealerGrantRecord>();
		if (dealers != null && dealers.size() > 0) {
			for (DealerGrantRecord dealer : dealers) {
				VDealerGrantRecord vdealergr = new VDealerGrantRecord();
				BeanUtils.copyProperties(dealer, vdealergr);
				if(dealer.getDealer()!=null){
					vdealergr.setDealerid(dealer.getDealer().getId());
				}
				if(dealer.getAgency()!=null){
					vdealergr.setAgencyid(dealer.getAgency().getId());
				}
				vdealergrs.add(vdealergr);
			}
		}
		return vdealergrs;
	}

	private List<DealerGrantRecord> find(VDealerGrantRecord vDealerGrantRecord) {
		String hql = "from DealerGrantRecord t where t.dealer.id=? ";

		List<Object> values = new ArrayList<Object>();
		values.add(vDealerGrantRecord.getDealerid());
		if (vDealerGrantRecord.getSort() != null
				&& vDealerGrantRecord.getOrder() != null) {
			hql += " order by " + vDealerGrantRecord.getSort() + " "
					+ vDealerGrantRecord.getOrder();
		}
		return dealerDao.find(hql, values, vDealerGrantRecord.getPage(),
				vDealerGrantRecord.getRows());
	}

	private Long total(VDealerGrantRecord vDealerGrantRecord) {
		String hql = "select count(*) from AgencyGrantRecord t where t.dealer.id=? ";
		List<Object> values = new ArrayList<Object>();
		values.add(vDealerGrantRecord.getDealerid());
		return dealerDao.count(hql, values);
	}

	@Override
	public void delete(String ids) {
		if (ids != null) {
			for (String id : ids.split(",")) {
				if (!id.trim().equals("0")) {
					DealerGrantRecord dealer = dealerDao.get(
							DealerGrantRecord.class, Long.parseLong(id.trim()));
					if (dealer != null) {
						dealerDao.delete(dealer);
					}
				}
			}
		}

	}

	@Override
	public Integer save(VDealerGrantRecord vDealerGrantRecord) {
		Integer result = 0;
		String hql = "from DealerGrantRecord t where t.agency.id=? and t.dealer.id=? ";

		List<Object> values = new ArrayList<Object>();
		values.add(vDealerGrantRecord.getAgencyid());
		values.add(vDealerGrantRecord.getDealerid());
		List<DealerGrantRecord> list = dealerDao.find(hql, values, 1, 1);
		if (list != null && list.size() > 0) {
			result = 1;
		}
		DealerGrantRecord dealerGrantRecord = new DealerGrantRecord();
		User agency = new User();
		agency.setId(vDealerGrantRecord.getAgencyid());
		User dealer = new User();
		dealer.setId(vDealerGrantRecord.getDealerid());
		dealerGrantRecord.setAgency(agency);
		dealerGrantRecord.setDealer(dealer);
		dealerDao.save(dealerGrantRecord);
		return result;

	}

	@Override
	public void grantConfirm(Long id) {
		DealerGrantRecord dgr = dealerDao.get(DealerGrantRecord.class, id);
		dgr.setGrantTime(new Date());
		dgr.setIsGranted(true);
		dealerDao.update(dgr);
	}

}
